proc auth {nick host handle arg} {
  global botnick home cryptkey bcast

 #correct handle
 #the asterisk is never a valid nick and is returned if a handle is not found.
 #corrects otbn.tcl mysterious userfile deletion bug
 if {$handle == "*"} {
   set handle $nick
 }

 #added for debugging purposes
 putlog "$nick ($handle) attempting to auth..."

 if {![validuser $handle]} { 
    puthelp "NOTICE $nick :\0034ERROR - Your user account could not be found in my user partition - you don't have access on me.\003"
    puthelp "NOTICE $nick :If you know you have access on this bot, please /msg $botnick ident YourPassword"
    puthelp "NOTICE $nick :Note that if you use the IDENT command, you MUST use the same nick as you were added to the bot with."
    puthelp "NOTICE $nick :You may discover this nick if you forgot it by typing (/msg) $botnick userlist or (/msg) $botnick userlist global"

    if {$bcast == "1"} {
      putquick "PRIVMSG $home :\0034\002$nick\002 who is connecting from $host tried to authenticate - no user account found.\003"  
    }

    return 0
  }

  if {$arg == ""} {
    putquick "NOTICE $nick :\0034ERROR - No Password Specified - You need to type your password.\003"
    return 0
  }

  if {[matchattr $handle D]} {
    putquick "NOTICE $nick :\0034ERROR - Unable To Authenticate - Your user account is disabled therefore you may not auth.\003"
    putlog "\002$handle\002 ($nick) tried to authenticate - the account is disabled - denied authentication."
  
    if {$bcast == "1"} {
      putquick "PRIVMSG $home :\0034\002$nick\002 who registered with the nick $handle and is connecting from $host tried to authenticate - the account is disabled - authentication was denied.\003"  
    }

    return 0
  }

  # is there a password set?
  if {[validuser $handle]} {
    set checkpw [getuser $handle PASS]

    if {$checkpw == ""} {
      putquick "NOTICE $nick :\0034ERROR - You don't have a password set - Please set a password using /msg $botnick PASS <password>\003"
      putquick "NOTICE $nick :Please note your password may not exceed 15 characters in length"
      putlog "$nick ($handle) attempted to auth but the user dosen't have a password set - prompting user to setpass."
      return 0
    }
  }

  set checkpw [lindex $arg 0]

  if {[passwdok $handle $checkpw]} {
    putlog "\002$handle\002 ($nick) sucessfully authenticated."
    putquick "NOTICE $nick :\0033Success - You are now authenticated on $botnick.\003"


    if {$bcast == "1"} {
      putquick "PRIVMSG $home :\0033\002$nick\002 who registered with the nick $handle and is connecting from $host authenticated sucessfully.\003"
    }

    chattr $handle +A
    save

    if [string match global [lindex $arg 1]] {
	putallbots "auth $nick $host $handle [encrypt $cryptkey $checkpw]"
	putquick "NOTICE $nick :Logging you on to all bots on botnet. Please Wait.."
    }

    return 0
  }
  
  putlog "\002$handle\002 ($nick) failed authentication."
  puthelp "NOTICE $nick :\0034ERROR - Unable To Authenticate - Failed to authenticate you - invalid password - check your password spelling and remember your password is CaSe SeNsaTive.\003"
  puthelp "NOTICE $nick :If you need your new hostmask added type \002/msg $botnick ident <your password>\002"
  puthelp "NOTICE $nick :Note that if you use the IDENT command, you MUST use the same nick as you were added to the bot with."
  puthelp "NOTICE $nick :You may discover this nick if you forgot it by typing (/msg) $botnick userlist or (/msg) $botnick userlist global"

  if {$bcast == "1"} {
    putquick "PRIVMSG $home :\0034\002$nick\002 who registered with the nick $handle and is connecting from $host failed authentication.\003"
  }

  chattr $handle -A
  save
}